//
//  ViewController.m
//  PayDemoIOS
//
//  Created by 杨红军 on 17/12/12.
//  Copyright © 2017年 zpon. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
#import "ServiceApi.h"
#import <AlipaySDK/AlipaySDK.h>
#import <Foundation/Foundation.h>
#import "WXApi.h"
#import "Payment.h"
#import "PayResult.h"


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark 自定义方法
- (IBAction)alipayEvent:(id)sender {
    [self fetchOrderInfo:@"alipay" amount:1 subject:@"商品"];
}

- (IBAction)wechatEvent:(id)sender {
    [self fetchOrderInfo:@"wx" amount:1 subject:@"商品"];
}

/**
 *获取订单信息
 */
- (void)fetchOrderInfo:(NSString*)channel amount:(int) amount subject:(NSString*)subject{
    [MBProgressHUD showMessage:@"订单获取中..." toView:self.view];
    NSDictionary *params = @{@"channel":channel,@"amount":@(amount),@"subject":@"商品"};
    __weak typeof(self) weakSelf = self;
    
    [[ServiceApi sharedServiceApi] fetchOrderInfoWithparams:params callback:^(NSDictionary *dic) {
        [MBProgressHUD hideHUDForView:weakSelf.view];
        // 开始支付
        [Payment createPaymentWithOrder:dic completion:^(PayResult *payresult) {
             NSLog(@"支付结果是:%@",payresult);
            if ([payresult.resultStatus isEqualToString:@"0"]) {
                  [MBProgressHUD showSuccess:@"支付成功"];
            }else if([payresult.resultStatus isEqualToString:@"-1"]){
                 [MBProgressHUD showError:@"支付失败"];
            }else if([payresult.resultStatus isEqualToString:@"-2"]){
                 [MBProgressHUD showError:@"支付取消"];
            }
        }];
    }];
}

@end
